import { ActionSpec } from "./ActionSpec";
import { ActionBuffers, IActionReceiver } from "./IActionReceiver";
import { IActuator } from "./IActuator";
import { BuiltInActuatorType, IBuiltInActuator } from "./IBuiltInActuator";
import { IDiscreteActionMask } from "./IDiscreteActionMask";
import { IHeuristicProvider } from "./IHeuristicProvider";

/**
 * 向量执行器
 */
export class VectorActuator implements IActuator, IBuiltInActuator {

    private _name: string = null!;

    private _actionSpec: ActionSpec = null!;

    private _actionReceiver: IActionReceiver = null!;

    private _heuristicProvider: IHeuristicProvider = null!;

    private _actionBuffers: ActionBuffers = null!;
    public get actionBuffers(): ActionBuffers {
        return this._actionBuffers;
    }
    public set actionBuffers(value: ActionBuffers) {
        this._actionBuffers = value;
    }


    get actionSpec(): ActionSpec {
        return this._actionSpec;
    }
    get name(): string {
        return this._name;
    }


    constructor(actionReceiver: IActionReceiver,
        heuristicProvider: IHeuristicProvider,
        actionSpec: ActionSpec,
        name: string = "VectorActuator") {
        this._actionReceiver = actionReceiver;
        this._heuristicProvider = heuristicProvider;
        this._actionSpec = actionSpec;

        let suffix;
        if (actionSpec.numContinuousActions == 0) {
            suffix = "-Discrete"; //离散
        }
        else if (actionSpec.numDiscreteActions == 0) {
            suffix = "-Continuous";
        }
        else {
            suffix = `-Continuous-${actionSpec.numContinuousActions}-Discrete-${actionSpec.numDiscreteActions}`;
        }

        this._name = name + suffix;
    }


    resetData(): void {
        // this._actionBuffers = empty;
    }

    onActionReceived(actionBuffers: ActionBuffers): void {
        this.actionBuffers = actionBuffers;
        this._actionReceiver.onActionReceived(this.actionBuffers);
    }

    writeDiscreteActionMask(actionMask: IDiscreteActionMask): void {
       this._actionReceiver.writeDiscreteActionMask(actionMask);
    }
    
    heuristic(actionBuffers: ActionBuffers): void {
       this._heuristicProvider?.heuristic(actionBuffers);
    }

    getBuiltInActuatorType(): BuiltInActuatorType {
        return BuiltInActuatorType.VectorActuator;
    }

}